#include "stdafx.h"

#include "StoreTable.h"

CStoreTbl::CStoreTbl()
{
	m_sName = _T("quan_ly_kho");
}
 bool CStoreTbl::addNewStore(const CStore* pStore)
 {
	 CString sQueryAdd;
	 sQueryAdd.Format(_T("( '%s',_utf8'%s', _utf8'%s', '%s','%s', _utf8'%s','%s',_utf8'%s')")
		 ,pStore->getCode(),pStore->getName(),pStore->getAddress(),pStore->getPhone(),
		 pStore->getFax(),pStore->getContact(),pStore->getMobile(),pStore->getNote());
	 CSqlTable::m_sName = TABLE_MAN_STORE;
	return insertRecord(TBL_STORE,sQueryAdd);
 }

 bool CStoreTbl::deleteStore(const CString& sStore)
 {
	 CString sValue;
	 bool bRes = false;
	 sValue.Format(_T("DELETE FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_MAN_STORE,FIELD_CODE_STORE,sStore);		
	 if (conn)
	 {
		 CT2A ascii(sValue, CP_UTF8);
		 if(mysql_query(conn,ascii.m_psz) == 0)
			 bRes = true;		
	 }
	 return bRes;
 }
 bool CStoreTbl::updateStore(const CStore* pStore)
 {
	 bool bResult = false;
	 if (pStore)
	 {
		 CString sQuery;
		 MYSQL_ROW row = NULL;
		 sQuery.Format(_T("UPDATE %s.%s SET "),g_sDatabase,TABLE_MAN_STORE);
		 CString stemp;
		 stemp.Format(_T("%s=_utf8'%s',"),FIELD_NAME_STORE,pStore->getName());
		 sQuery += stemp;
		 stemp.Format(_T("%s=_utf8'%s',"),FIELD_ADRESS_STORE,pStore->getAddress());
		 sQuery += stemp;
		 stemp.Format(_T("%s= '%s',"),FIELD_PHONE_STORE,pStore->getPhone());
		 sQuery += stemp;
		 stemp.Format(_T("%s='%s',"),FIELD_FAX_STORE,pStore->getFax());
		 sQuery += stemp;
		 stemp.Format(_T("%s=_utf8'%s',"),FIELD_CONTACT_STORE,pStore->getContact());
		 sQuery += stemp;
		 stemp.Format(_T("%s='%s',"),FIELD_MOBILE_STORE,pStore->getMobile());
		 sQuery += stemp;
		 stemp.Format(_T("%s=_utf8'%s'"),FIELD_NOTE_STORE,pStore->getNote());
		 sQuery += stemp;		 

		 CString sAddition;
		 sAddition.Format(_T(" WHERE %s ='%s'"),FIELD_CODE_STORE,pStore->getCode());

		 sQuery += sAddition;	
		 CT2A ascii(sQuery, CP_UTF8);	
		 if(mysql_query(conn,ascii.m_psz)==0)
			 bResult = true;
	 }
	 return bResult;
 }

CStore* CStoreTbl::getStore(CString sCode)
{
	CStore *pStore = NULL;
	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s where %s ='%s'"),g_sDatabase,TABLE_MAN_STORE,FIELD_CODE_STORE,sCode);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{
			pStore = new CStore;
			pStore->setCode(sCode);
			pStore->setName(CString (CA2W(row[1], CP_UTF8)));
			pStore->setAddress(CString (CA2W(row[2], CP_UTF8)));
			pStore->setPhone(CString(row[3]));
			pStore->setFax(CString(row[4]));
			pStore->setContact(CString (CA2W(row[5], CP_UTF8)));
			pStore->setMobile(CString(row[6]));
			pStore->setNote(CString (CA2W(row[7], CP_UTF8)));
		}
		mysql_free_result(pResult);
	}
	return pStore;
}

 vector<CStore*> CStoreTbl::getListStore()
 {
	 vector<CStore*> vtStore;

	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s "),g_sDatabase,TABLE_MAN_STORE);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = NULL;
		 while((row = mysql_fetch_row(pResult)) )
		 {
			 CStore* pStore = new CStore;
			 pStore->setCode(CString(row[0]));
			 pStore->setName(CString (CA2W(row[1], CP_UTF8)));
			 pStore->setAddress(CString (CA2W(row[2], CP_UTF8)));
			 pStore->setPhone(CString(row[3]));
			 pStore->setFax(CString(row[4]));
			 pStore->setContact(CString (CA2W(row[5], CP_UTF8)));
			 pStore->setMobile(CString(row[6]));
			 pStore->setNote(CString (CA2W(row[7], CP_UTF8)));
			 vtStore.push_back(pStore);
		 }
		 mysql_free_result(pResult);
	 }
	 return vtStore;
 }
 int  CStoreTbl::getMaxCode()
 {
	 int nMax = 0;
	 MYSQL_RES * pResult = NULL;

	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT %s ,MAX(%s)FROM %s.%s "),FIELD_CODE_STORE,FIELD_CODE_STORE,g_sDatabase,TABLE_MAN_STORE);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = mysql_fetch_row(pResult);
		 if (row)
		 {
			 CString sTemplate;
			 sTemplate = row[1];
			 sTemplate.Replace('K','0');
			 nMax = _tstoi(sTemplate);
		 }
		mysql_free_result(pResult);
	 }
	 return nMax;
 }
